﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using Studio77.PopBall.Core;
using Studio77.PopBall.Util;

namespace Studio77.PopBall.Views
{
    public partial class GameItem : UserControl
    {
        public int Type { get; set; }

        public bool IsSelected { get; set; }

        public GameItem()
        {
            InitializeComponent();
        }

        public void SetType(int type)
        {
            this.StoryboardBlink.Stop();
            if (type != 0 && type != 100)
            {
                this.imgItem.Visibility = System.Windows.Visibility.Visible;
                this.StoryboardResume.Begin();
                IsSelected = false;
                Canvas.SetZIndex(this, 1);
                string uri = "/Images/ball" + type + ".png";
                Type = type;

                BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
                imgItem.Source = bitmap;
            }
            else
            {
                this.imgItem.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        public void SetSelected()
        {
            this.selector.Visibility = System.Windows.Visibility.Visible;
            IsSelected = true;
            this.StoryboardSelect.Begin();
        }

        public void SetUnSelected()
        {
            this.selector.Visibility = System.Windows.Visibility.Collapsed;
            IsSelected = false;
        }

        internal void Eliminate(int beginTime, int eliminateIndex, bool isNeedSound = true)
        {
            //音效Here
            Type = 0;
            this.selector.Visibility = System.Windows.Visibility.Collapsed;
            StoryboardEliminate.BeginTime = TimeSpan.FromMilliseconds(beginTime);
            StoryboardEliminate.Begin();

            if (isNeedSound)
            {
                //延时声音
                MyDispatcherTimer tempTimer = new MyDispatcherTimer();
                tempTimer.Interval = TimeSpan.FromMilliseconds(beginTime);
                tempTimer.Tick += (object ss, EventArgs ee) =>
                {
                    tempTimer.Stop();

                    SoundManager.PlayEffect(SoundManager.exp);

                    //if (eliminateIndex == 0)
                    //{
                    //    SoundManager.PlayEffect(SoundManager.exp);
                    //}
                    //else
                    //{
                    //    int temp = eliminateIndex % 4;
                    //    if (temp == 0)
                    //    {
                    //        SoundManager.PlayEffect(SoundManager.exp0);
                    //    }
                    //    else if (temp == 1)
                    //    {
                    //        SoundManager.PlayEffect(SoundManager.exp1);
                    //    }
                    //    else if (temp == 2)
                    //    {
                    //        SoundManager.PlayEffect(SoundManager.exp3);
                    //    }
                    //    else if (temp == 3)
                    //    {
                    //        SoundManager.PlayEffect(SoundManager.exp3);
                    //    }
                    //}
                };
                tempTimer.Start();
            }

        }

        internal void Bounce()
        {
            this.StoryboardBounce.Begin();
        }

        internal void ChangeType(int type)
        {
            Type = type;
            this.StoryboardHide.BeginTime = TimeSpan.FromMilliseconds(50);
            this.StoryboardHide.Completed += StoryboardHide_Completed;
            this.StoryboardHide.Begin();
        }

        void StoryboardHide_Completed(object sender, EventArgs e)
        {
            this.StoryboardHide.Completed -= StoryboardHide_Completed;
            SetType(Type);
            this.StoryboardShow.BeginTime = TimeSpan.FromMilliseconds(77);
            this.StoryboardShow.Begin();
        }

        internal void Blink()
        {
            this.StoryboardBlink.RepeatBehavior = new System.Windows.Media.Animation.RepeatBehavior(3);
            this.StoryboardBlink.Begin();
        }
    }
}
